//workspace
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import java.io.File;
import java.awt.Color;

public class ChanceUI extends JFrame {

	
	private JPanel contentPane;
	private JLabel imageLabel;
	private ImageIcon image;
	
	private int number;
	private int position;
	private int ownerPropCount;
	private int PropCountTotal;
	private Scanner scanner;
	private Cards[] cards;
	private Player player;
	private Player[] pArray;
	private Block[] bl;
	private Board b;
	private PropertyBlock[] propBlkTemp;
	private PropertyBlock[] propertyBlk;
	private Random random;
	
	private static ChanceUI singleChanceUI;
	
	protected ChanceUI(Player player, Block[] blocks, Player[] playerArray) {
		this.player = player;
		this.bl = blocks;
		this.pArray = playerArray;
		this.cards = new Cards[16];
		ArrayList<PropertyBlock> propertyBlockArrayList = new ArrayList<PropertyBlock>();
		for(int i=0; i<bl.length;i++){//get number of property blocks
			if(this.bl[i] instanceof PropertyBlock){
				propertyBlockArrayList.add((PropertyBlock) this.bl[i]);
			}
		}
		
		//propBlkTemp = new PropertyBlock[PropCountTotal];//create array of propertyBlocks
		
		this.propertyBlk = new PropertyBlock[propertyBlockArrayList.size()];
		propertyBlockArrayList.toArray(this.propertyBlk);
		
		propertyBlockArrayList.clear();
		
		for(int i=0; i<this.propertyBlk.length; i++){
			
			if(this.propertyBlk[i].getCOwner()==player){
				
				propertyBlockArrayList.add(this.propertyBlk[i]);
				
			}
			
		}
		
		this.propertyBlk = new PropertyBlock[propertyBlockArrayList.size()];
		propertyBlockArrayList.toArray(this.propertyBlk);
		
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(10, 10, 723, 557);
		contentPane = new JPanel();
	
		this.openFile();
		this.readFile();
		this.closeFile();	
		
	}
	
	
	 public static ChanceUI getInstance(Player play, Block[] blks, Player[] pArray){
		  if(singleChanceUI==null){
			  singleChanceUI=new ChanceUI(play, blks, pArray);
		  }
		  
		  return singleChanceUI;
	  }
	  
	
	private void exitButtonPressed(ActionEvent extEvent) {
		this.execute();
		this.dispose();
	
	}
	
	public void drawCard(){
		
		do{
		random = new Random();
		this.number=random.nextInt(16);
		}while(number == 7 && cards[7].getPlayer().getPlayerName() != null);
		
		this.image = new ImageIcon(cards[number].getCardImage());
		this.setTitle(cards[number].getCardType());
		
		if(this.imageLabel!=null){
			this.contentPane.setVisible(false);
			this.imageLabel.setVisible(false);
			this.contentPane.remove(imageLabel);
		}
		
		this.imageLabel = new JLabel(this.image);
		this.imageLabel.setBackground(Color.WHITE);
		this.imageLabel.setBounds(8, 5, 690, 450);
		this.imageLabel.setVisible(true);
		contentPane.add(this.imageLabel);
		
		System.out.println(image);
			
		JButton exitButton = new JButton("Okay");
		exitButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent extEvent) {
				exitButtonPressed(extEvent);
			}

		});
		
		exitButton.setBounds(310, 466, 116, 30);
		contentPane.add(exitButton);	
		
		setContentPane(contentPane);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setVisible(true);
		contentPane.setLayout(null);
	}
	
	public void openFile(){
		try{
			scanner = new Scanner(new File("src/chance.txt"));
			scanner.useDelimiter(",");
		}catch(Exception e){
			System.out.println("Error Reading File");
		}
	}

	public void readFile(){
		int counter=0;
		do{
			
				if(counter == 6 || counter == 11 || counter == 14 || counter == 15){
					this.cards[counter] = new Cards(scanner.next(),Integer.parseInt(scanner.next()), scanner.next(), Integer.parseInt(scanner.next()));
				}else if(counter == 10){
					this.cards[counter] = new Cards(scanner.next(),Integer.parseInt(scanner.next()), scanner.next(), Integer.parseInt(scanner.next()), Integer.parseInt(scanner.next()));
				}else if(counter == 7){
					this.cards[counter] = new Cards(scanner.next(),Integer.parseInt(scanner.next()), scanner.next(), this.player);
				}else{
					this.cards[counter]= new Cards(scanner.next(),Integer.parseInt(scanner.next()), scanner.next());
				}
				
				counter++;
			
		}while(scanner.hasNext());
		
	}
	
	
	public void closeFile(){
		scanner.close();
	}
	
	
	public void execute(){
		
		position = player.getCurrentBlockPos().getBlockNumber();
		Command moveCmd = new MovePlayerCommand();
		Command moveBackCmd = new MoveBackCommand();
		Command nextCmd = new NextPlayerCommand();
		Command jailCmd = new SendCurrentPlayerToJailCommand();
		CommandWithReturn bankruptcyCmd = new BankruptcyCheckerCommand();
		CommandWithInt cmdInt = new PropertySelectionUICommand();
		Command transferCmd = new TransferAssetsCommand();
		
		switch(number){
		
			case 0: 				
				
				if(position == 7){
					player.setLastRoll(33);
				}else if(position == 22){
					player.setLastRoll(18);
				}else if(position == 36){
					player.setLastRoll(4);
				}
				
				moveCmd.execute();			
				break;
				
			case 1: 
				
				if(position == 7){
					player.setLastRoll(17);
				}else if(position == 22){
					player.setLastRoll(2);
				}else if(position == 36){
					player.setLastRoll(28);
				}
				
				moveCmd.execute();
				break;
				
			case 2: 
				
				if(position == 7){
					player.setLastRoll(4);
				}else if(position == 22){
					player.setLastRoll(29);
				}else if(position == 36){
					player.setLastRoll(15);
				}
				
				moveCmd.execute();				
				break;
				
			case 3: 
				
				if(position == 7){
					player.setLastRoll(8);
				}else if(position == 22){
					player.setLastRoll(3);
				}else if(position == 36){
					player.setLastRoll(9);
				}
				
				moveCmd.execute();				
				break;
				
			case 4: 
				
				if(position == 7){
					player.setLastRoll(8);
				}else if(position == 22){
					player.setLastRoll(3);
				}else if(position == 36){
					player.setLastRoll(9);
				}
				
				moveCmd.execute();				
				break;
				
			case 5: 
				
				if(position == 7){
					player.setLastRoll(5);
				}else if(position == 22){
					player.setLastRoll(6);
				}else if(position == 36){
					player.setLastRoll(16);
				}
				
				moveCmd.execute();				
				break;
				
			case 6: 
				
				player.addMoney(cards[6].getAmount());
				JOptionPane.showMessageDialog(null, "Kumita ka ng " + cards[6].getAmount() + "Php. Ang pera mo na ngayon ay " + player.getCurrentMoney());
				nextCmd.execute();
				break;
				
			case 7:
				
				player.setChanceJailToken(1);
				nextCmd.execute();
				
				break;
				
			case 8:
				
				moveBackCmd.execute();
				
				break;
				
			case 9:
				
				jailCmd.execute();
				nextCmd.execute();
				break;
				
			case 10:

				int houses = 0;
				int hotels = 0;
				int totalHotelAmount;
				int totalHouseAmount;
				
				for(int counter = 0; counter<this.propertyBlk.length; counter++){
					if(propertyBlk[counter].getCurrentNumberOfHouses() == 5){
						hotels = hotels+1;
					}else{
						houses = houses+propertyBlk[counter].getCurrentNumberOfHouses();
					}
				}
				
				totalHouseAmount = houses * cards[10].getAmount();
				totalHotelAmount = hotels * cards[10].getHotelAmount();
				int totalPayment = totalHouseAmount+totalHotelAmount;
					
				if(totalPayment != 0){	
					if(player.getCurrentMoney()<totalHouseAmount || player.getCurrentMoney()<totalHotelAmount){
						bankruptcyCmd.execute();
						if(bankruptcyCmd.execute() == 0){
							JOptionPane.showMessageDialog(null, "Wala ka nang pera kahit ibenta mo pa lahat ng pag-aari mo. Natalo ka sa laro, sir.");
							transferCmd.execute();
							this.player.setActive(false);
						}else{
							JOptionPane.showMessageDialog(null, "Wala ka nang pera pero pwede mong ibenta ang mga pag-aari mo.");
							cmdInt.execute(2);
						}
					}else{
						player.subtractMoney(totalPayment);
						JOptionPane.showMessageDialog(null, "Nagbayad ka ng " + totalPayment + "Php. Ang pera mo na ngayon ay " + player.getCurrentMoney() + "Php.");
						}
				}else{
					JOptionPane.showMessageDialog(null, "Wala ka pang mga bahay at hotel. Ang pera mo ay " + player.getCurrentMoney() + "Php pa rin.");
				}
							
						nextCmd.execute();	
				break;
				
			case 11:
				
				if(player.getCurrentMoney()<cards[11].getAmount()){
					bankruptcyCmd.execute();
					if(bankruptcyCmd.execute() == 0){
						JOptionPane.showMessageDialog(null, "Wala ka nang pera kahit ibenta mo pa lahat ng pag-aari mo. Natalo ka sa laro, sir.");
						transferCmd.execute();
						this.player.setActive(false);
					}else{
						JOptionPane.showMessageDialog(null, "Wala ka nang pera pero pwede mong ibenta ang mga pag-aari mo.");
						cmdInt.execute(2);
					}
				}else{
					player.subtractMoney(cards[11].getAmount());
					JOptionPane.showMessageDialog(null, "Nagbayad ka ng " + cards[11].getAmount() + "Php. Ang pera mo na ngayon ay " + player.getCurrentMoney() + "Php.");
				}
				nextCmd.execute();
				break;
				
			case 12: 
				
				if(position == 7){
					player.setLastRoll(38);
				}else if(position == 22){
					player.setLastRoll(23);
				}else if(position == 36){
					player.setLastRoll(9);
				}
			
				moveCmd.execute();				
				break;
				
			case 13: 
				
				if(position == 7){
					player.setLastRoll(32);
				}else if(position == 22){
					player.setLastRoll(17);
				}else if(position == 36){
					player.setLastRoll(4);
				}
			
				moveCmd.execute();
				break;
				
			case 14: 
				
				player.addMoney(cards[14].getAmount());
				JOptionPane.showMessageDialog(null, "Kumita ka ng " + cards[14].getAmount() + "Php. Ang pera mo na ngayon ay " + player.getCurrentMoney() + "Php.");	
				nextCmd.execute();
				break;
				
			case 15:
				
				if(this.pArray.length>1){
					int totalPlayerPayment = (this.pArray.length-1) * cards[15].getAmount();
					for(int counter = 0; counter<this.pArray.length; counter++){
						if(pArray[counter] != this.player){
							pArray[counter].addMoney(cards[15].getAmount());
						}
					}
					
					if(player.getCurrentMoney()<totalPlayerPayment){
						bankruptcyCmd.execute();
						if(bankruptcyCmd.execute() == 0){
							JOptionPane.showMessageDialog(null, "Wala ka nang pera kahit ibenta mo pa lahat ng pag-aari mo. Natalo ka sa laro, sir.");
							transferCmd.execute();
							this.player.setActive(false);
						}else{
							JOptionPane.showMessageDialog(null, "Wala ka nang pera pero pwede mong ibenta ang mga pag-aari mo.");
							cmdInt.execute(2);
						}
					}else{
						player.subtractMoney(totalPlayerPayment);
						JOptionPane.showMessageDialog(null, "Binayaran mo ang bawat manlalaro ng " + cards[15].getAmount() + "Php. Ang pera mo ngayon ay " + player.getCurrentMoney() + "Php.");
					}
					nextCmd.execute();
				}else{
					JOptionPane.showMessageDialog(null, "Wala kang ibang kalaro. Ang pera mo ay  " + player.getCurrentMoney() +"Php pa rin.");
					nextCmd.execute();
				}
				break;

		}
		
		
	}
}

